<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>维护管理</el-breadcrumb-item>
                <el-breadcrumb-item>维修厂</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="shopFormData"  class="demo-form-inline" >
                <el-form-item >
                    <el-input v-model="shopFormData.name" placeholder="请输入维修厂名称"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="chaxun">查询</el-button>
                    <el-button type="primary" @click="insert">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="编号"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="name"
                            label="维修厂名"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="address"
                            label="地址">
                    </el-table-column>
                    <el-table-column
                            prop="phoneNum"
                            label="联系电话">
                    </el-table-column>
                    <el-table-column
                            prop="status"
                            label="合作状态">
                        <template slot-scope="scope">
                            <el-switch
                                    v-model="scope.row.status"
                                    :active-value="0"
                                    active-color="#13ce66"
                                    :inactive-value="1"
                                    inactive-color="#ff4949"
                                    @change="switchStatus(scope.row.id,scope.row.status)">
                            </el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="addtime"
                            label="建立时间">
                    </el-table-column>
                    <el-table-column
                            prop="remark"
                            label="建议">
                    </el-table-column>

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="selectById(scope.row.id)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteById(scope.row.id)"></el-button>
                            <!--                            <el-button type="warning" icon="el-icon-star-off" circle @click="showUserRole(scope.row.userId)"></el-button>-->
                        </template>
                    </el-table-column>
                </el-table>

                <!--                分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>
        </el-card>
        <el-dialog
                title="提示"
                :visible.sync="editDialogVisible"
                width="30%"
        >
            <!--            model:表单数据-->
            <el-form  label-width="80px" :model="shopForm"  :rules="FormRules" ref="shopForm">
                <el-form-item label="id" style="display: none">
                    <el-input v-model="shopForm.id" ></el-input>
                </el-form-item>
                <el-form-item label="名称"  prop="name" >
                    <el-input v-model="shopForm.name" placeholder="请输入名称" ></el-input>
                </el-form-item>

                <el-form-item label="地址" >
                <el-cascader

                        size="large"
                        :options="options"
                        v-model="selectedOptions"
                        @change="handleChange">
                </el-cascader>
                </el-form-item>
                <el-form-item label="联系电话" prop="phoneNum"  >
                    <el-input v-model="shopForm.phoneNum" placeholder="请输入联系电话"></el-input>
                </el-form-item>
                <el-form-item label="时间"  prop="addtime">
                    <el-form-item >
                        <el-date-picker
                                v-model="shopForm.addtime"
                                type="date"
                                placeholder="日期">
                        </el-date-picker>
                    </el-form-item>
                </el-form-item>
                <el-form-item label="建议"  >
                    <el-input v-model="shopForm.remark" placeholder="请给出建议"></el-input>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="formSubmit">确 定</el-button>
                <el-button @click="editDialogVisible = false">取 消</el-button>

              </span>
        </el-dialog>
    </div>
</template>

<script>
    import {  regionData,CodeToText,TextToCode } from 'element-china-area-data'

    export default {
        name: "Shop",
        data(){

            //数字自定义校验
            const validateAcquaintance = (rule, value, callback) => {
                value = Number(value)
                if (typeof value === 'number' && !isNaN(value)) {
                    callback()
                } else {
                    callback(new Error('必须为数字'))
                }
            }

            return {
                options: regionData,
                selectedOptions: [],
                form:{
                    provinces:"",
                    city:"",
                    area:""
                },
                //提示框
                editDialogVisible:false,
                //条件搜索对象
                shopFormData:{},
                //表格数据
                tableData:[],
                //每页显示的条数
                pageSize: 5,
                //当前的显示的页码
                currentPage:1,
                //总条数
                total:0,
                //添加表:
                shopForm:{},
                //表单校验
                FormRules:{
                    name:[
                        {required:true,message:"名称不能为空",trigger:"blur"}
                    ],
                    address:[
                        {required:true,message:"地址不能为空",trigger:"blur"}
                    ],
                    phoneNum:[
                        {required:true,message:"联系电话不能为空",trigger:"blur" },
                        { max: 11, message: '请输入11 个数字', trigger: 'blur' },
                        { validator: validateAcquaintance, trigger: 'blur'}
                    ],


                }

            }
        },
        created() {
            this.selectShopAll();

        },
        methods:{
            handleChange (value) {

                console.log(value)
                if (value[1] != null && value[2] != null) {
                    var dz = CodeToText[value[0]] + '/' + CodeToText[value[1]] + '/' + CodeToText[value[2]]

                } else {
                    if (value[1] != null) {
                        dz = CodeToText[value[0]] + '/' + CodeToText[value[1]]
                    } else {
                        dz = CodeToText[value[0]]
                    }
                }
                this.shopForm.address = dz

                this.form.provinces= CodeToText[this.selectedOptions[0]]
                this.form.city= CodeToText[this.selectedOptions[1]]
                this.form.area= CodeToText[this.selectedOptions[2]]
            },

            insert(){
                this.editDialogVisible=true;
                this.selectedOptions=[]
                this.shopForm={}
                // this.$refs.shopForm.resetFields();
            },
            deleteById(id){

                var that=this;
                this.$http.delete(`shop/delete/${id}`).then(function (returnResult) {
                    that.$message.success("删除成功");
                    that.selectShopAll();
                })
            },
            selectById(id){
                var that=this;

                this.$http.get(`shop/selectById/${id}`).then(function (returnResult) {
                    console.log(returnResult.data.result.tbshop.address)
                    let strings=returnResult.data.result.tbshop.address.split("/")

                    that.form.provinces=strings[0];
                    that.form.city=strings[1];
                    that.form.area=strings[2];
                    that.selectedOptions=[
                        TextToCode[that.form.provinces].code,
                        TextToCode[that.form.provinces][that.form.city].code,
                        TextToCode[that.form.provinces][that.form.city][that.form.area].code
                    ]
                    that.editDialogVisible=true;
                    that.shopForm=returnResult.data.result.tbshop

                })
                // this.$refs.shopForm.resetFields();
                this.shopForm={}


            },
            formSubmit(){

                var that=this;

                console.log(this.shopForm.address)
                if(this.shopForm.id!=null){
                    this.$http.put("shop/update",this.shopForm).then(function (returnResult) {
                        that.editDialogVisible=false;
                        that.shopForm={};
                        that.selectShopAll();
                    })
                }else {
                    this.$http.post("shop/insert", this.shopForm).then(function (returnResult) {
                        that.editDialogVisible = false;
                        that.selectShopAll();
                    })
                }
            },
            chaxun(){
                this.selectShopAll();
            },
            selectShopAll(){
                var that=this;
                this.$http.post(`/shop/selectPage/${this.currentPage}/${this.pageSize}`,this.shopFormData).then(function (returnResult) {
                    console.log(returnResult)
                    //表格数据
                    that.tableData=returnResult.data.result.records;
                    //总条数
                    that.total=returnResult.data.result.total;
                })
            },
            handleSizeChange(v){
                this.pageSize=v;
                this.selectShopAll();
            },
            handleCurrentChange(v){
                this.currentPage=v;
                this.selectShopAll();
            },
            switchStatus(id,status){
                var that=this;
                this.$http.put(`shop/updateStatus/${id}/${status}`).then(function (response) {
                    if (status=="1"){
                        that.$message.warning("终止合作")
                    }else {
                        that.$message.success("开始合作")
                    }
                })
            }

        }
    }
</script>

<style scoped>

</style>